package cn.ubeta.b2c.bean;

import android.os.Parcel;
import android.os.Parcelable;

import cn.ubeta.b2c.DemoData;

import java.util.ArrayList;
import java.util.List;

import static cn.ubeta.b2c.DemoData.price;

/**
 * Created by liudengyong on 16/8/17.
 */

public class ProductInfo extends ApiResp implements Parcelable {

    long skuId;
    String name;
    String desc;
    String imgUrl;
    int listImg;
    int[] detailImg;
    int brandId;
    boolean model;
    private PriceInfo priceInfo;

    /**
     * 预植数据构造
     */
    public ProductInfo(long skuId, String name, String desc, int brandId, int listImg, int[] detailImg, boolean model) {
        this.skuId = skuId;
        this.name = name;
        this.desc = desc;
        this.brandId = brandId;
        this.listImg = listImg;
        this.detailImg = detailImg;
        this.model = model;
    }

    // TODO 待删除
    public ProductInfo(long skuId) {
        //this(skuId, DemoData.name[i], DemoData.desc[i], price[i]);
    }

    public String getDesc() {
        return desc;
    }

    public static List<ProductInfo> getTestData(int catgId) {
        List<ProductInfo> data = new ArrayList<>();
        int len = catgId > 0 ? 4 : 12;
        for (int i = 0; i < len; ++i) {
            data.add(new ProductInfo(i, DemoData.name[i], DemoData.desc[i], price[i]));
        }
        return data;
    }

    public void setPriceInfo(PriceInfo priceInfo) {
        this.priceInfo = priceInfo;
    }

    public int getBrandId() {
        return brandId;
    }

    public String getDisplayPrice() {
        if (priceInfo == null || priceInfo.getStrPrice().startsWith("-1.0"))
            return "暂无价格";
        else
            return "￥ " + priceInfo.getStrPrice();
    }

    public double getPrice() {
        //return "￥ " + priceInfo.getPrice();
        return priceInfo == null ? 0 : priceInfo.getPrice();
    }

    public static class ImageInfo {
        public String imgUrl;
        // TODO 图片信息
    }

    public ProductInfo(long skuId, String name, String desc, double price) {
        // TODO 调试:待删除
        this.skuId  = skuId;
        this.name = name;
        this.desc = desc;
        priceInfo = new PriceInfo();
        priceInfo.setPrice(price);
    }

    public ProductInfo(Parcel in) {
        skuId = in.readLong();
        brandId = in.readInt();
        listImg = in.readInt();
        priceInfo = new PriceInfo();
        priceInfo.setPrice(in.readDouble());
        priceInfo.setMprice(in.readDouble());
        name = in.readString();
        imgUrl = in.readString();
        name = in.readString();
        desc = in.readString();
        detailImg = in.createIntArray();
        model = in.readInt() == 1;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(skuId);
        parcel.writeInt(brandId);
        parcel.writeInt(listImg);
        parcel.writeDouble(priceInfo == null ? 0 : priceInfo.getPrice());
        parcel.writeDouble(priceInfo == null ? 0 : priceInfo.getMprice());
        parcel.writeString(name);
        parcel.writeString(imgUrl);
        parcel.writeString(name);
        parcel.writeString(desc);
        parcel.writeIntArray(detailImg);
        parcel.writeInt(hasModel() ? 1 : 0);
    }

    public static final Creator<ProductInfo> CREATOR = new Creator<ProductInfo>() {
        @Override
        public ProductInfo createFromParcel(Parcel in) {
            return new ProductInfo(in);
        }

        @Override
        public ProductInfo[] newArray(int size) {
            return new ProductInfo[size];
        }
    };

    public long getSkuId() {
        return skuId;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public int getListImg() {
        return listImg;
    }

    public String getName() {
        return name;
    }

    @Override
    public int describeContents() {
        // TODO 序列号
        return 0;
    }

    public PriceInfo getPriceInfo() {
        return priceInfo;
    }

    public int[] getDetailImg() {
        return detailImg;
    }

    public boolean hasModel() {
        return true;
    }
}
